/**new patch from icfoss*/
/*header files*/

#include<stdio.h>
#include<stdlib.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
struct dates{
	char d[25];
};
class hotel{
	public:
	char name[25];
	int room_id;
	dates wdate;
	dates cdate;
	int days;
	float adv;
	char room_type;
	char phone[15];
	char address[50];
	void putdata();
	void getdata(int);
	int roomstatus(int r);

};
class menus
{
public:
void mainmenu();
void insertmenu();
void modifymenu();
void searchmenu();
void deletemenu();
void  viewmenu();
void freeroom();

int getpos(int);
};

class control:public menus
{       clrscr();
	public:
	int ch;
	void controller()
	{
		ch=0;
		menus obj;
		obj.mainmenu();
		cin>>ch;
		do{
		switch(ch)
		{
			case 1:insertmenu();
			break;
			case 2:modifymenu();
			break;
			case 3:searchmenu();
			break;
			case 4:viewmenu();
			break;
			case 5:deletemenu();
			break;
			case 6:freeroom();
			break;
			case 7:exit(0);
			default:
			cout<<"\nWrong choice...........";
		}
		cin>>ch;
	}while (ch<7);
}
};
void hotel:: getdata(int ro)
	{//function to read records
	room_id=ro;
	cout<<"\nName: ";
	cin>>name;
	fflush(stdin);
	cout<<"\nPhone: ";
	cin>>phone;
	cin.get();
	cout<<"\nDate of booking: ";
	cin>>wdate.d;
	cout<<"\nDate of check in: ";
	cin>>cdate.d;
	cout<<"\nNo of days to stay:";
	cin>>days;
	cout<<"\nRoom type (S=Single / D=Double /T= Tripple) bed: ";

	if(ro>299) room_type='T';
	if(ro>199&& ro<299) room_type='D';
	if(ro>99&& ro<199) room_type='S';
	cout<<room_type;
	cout<<"\n\nAddress: ";
	cin>>address;
	cout<<"\nAdvance: ";
	cin>>adv;
	}
void hotel:: putdata()
	{ //function to output records
	cout<<"\nName: ";
	cout<<name;
	cout<<"\nRoom_id: ";
	cout<<room_id;
	cout<<"\nPhone: ";
	cout<<phone;
	cout<<"\nDate of booking: ";
	cout<<wdate.d;
	cout<<"\nDate of check in: ";
	cout<<cdate.d;
	cout<<"\nNo of days to stay:";
	cout<<days;
	cout<<"\nRoom type (S=Single / D=Double /T= Tripple) bed: ";
	cout<<room_type;
	cout<<"\nAddress: ";
	cout<<address;
	cout<<"\nAdvance: ";
	cout<<adv;
	}
int hotel::roomstatus(int i)
{       //function to check room status
	fstream file;
	hotel item;
	int flag=0;
	file.open("record.data",ios::in);
	while(file.read((char*)&item,sizeof(hotel)))
	{
	if(item.room_id==i)
	{ flag=1;
	}

	}
	file.close();
	if(flag==1)
	return -1;
return 1;
}
void menus::freeroom()
{
clrscr();
cout<<"\n--------------------------------------------";
cout<<"\nType of rooms";
cout<<"\n--------------------------------------------";
cout<<"\nSingle Rooms:  100, 101, 102, 103, 104, 105";
cout<<"\nDouble Rooms:  200, 201, 202, 203";
cout<<"\nTripple Rooms: 300, 301, 302, 303";
cout<<"\n--------------------------------------------";
getch();
mainmenu();
}

void menus::mainmenu()
{       //function to display mainmenu
	clrscr();
	cout<<"\n-------------------------";
	cout<<"\nHOTEL RESERVATION SYSTEM";
	cout<<"\n-------------------------" ;
	cout<<"\n\t1.Insert Record";
	cout<<"\n\t2.Modify Record";
	cout<<"\n\t3.Search Record";
	cout<<"\n\t4.View   Record";
	cout<<"\n\t5.Delete Record";
	cout<<"\n\t6.Help";
	cout<<"\n\t7.Exit";
	cout<<"\n-------------------------";
	cout<<"\n\nEnter your choice: ";
}
void menus::insertmenu()
{       //function to display insertmenu
	clrscr();
	cout<<"\n-------------------------";
	cout<<"\nInserting Record";
	cout<<"\n-------------------------";
	hotel item;
	fstream file;
	int rid;


	cout<<"\nRoom_id: ";
	cin>>rid;
	if(!((rid>99 && rid<111)||(rid>199 && rid<205)||(rid>299 && rid<305)))
		{
		cout<<"\nInvalid room no";
		}

    else{
	int f=item.roomstatus(rid);
	if(f==1)
	{
	item.getdata(rid);
	file.open("record.data",ios::out|ios::app|ios::ate|ios::binary);
	file.write((char*)&item,sizeof(hotel));

	file.close();
	}
	if(f==-1) cout<<"\nRooms already in Use";
	}
	getch();
	clrscr();
	mainmenu();
}
int menus::getpos( int no)
{       //returns the position of record
	cout<<"\nDetails\n";
	fstream file ;hotel b;
    file.open("record.data", ios::in) ;
    file.seekg(0) ;
    int count = 0 ;
    while (file.read((char *) &b, sizeof(hotel)))
    {
	count++ ;
	if (b.room_id == no)
	{
			cout<<"\nRoom_id:";
			cout<<b.room_id;
			cout<<"\nName:";
			cout<<b.name;
	    break ;
		}
    }
    file.close() ;
	cout<<endl;
    return count ;
}

void menus::modifymenu()
{       //function to modify records
	clrscr();
	cout<<"\n-------------------------";
	cout<<"\nModify Record";
	cout<<"\n-------------------------" ;
int rid,se;
fstream f;
f.open("record.data",ios::in|ios::out|ios::binary);
hotel b1;


					cout<<"\nEnter room no: ";
					cin>>rid;
					if(((rid>99 && rid<111)||(rid>199 && rid<205)||(rid>299 && rid<305)))
				{
						se=b1.roomstatus(rid);
						if(se==-1)
						{int l=getpos(rid);
						cout<<"\nEnter new room no: ";
						cin>>rid;
							if(((rid>99 && rid<111)||(rid>199 && rid<205)||(rid>299 && rid<305)))
							{

					long pos=(l-1) * sizeof(hotel);

					b1.getdata(rid);
					f.seekg(pos,ios::beg);
					f.write((char *)&b1,sizeof(hotel));
					f.close();
					}
					else {cout<<"\nInvalid room";}
				}
				}else {cout<<"\nInvalid room";}



getch();
clrscr();

mainmenu();
}
void menus::searchmenu()
{       //function to search records
	clrscr();
	cout<<"\n-------------------------";
	cout<<"\nSearching Record";
	cout<<"\n-------------------------";
	cout<<"\nEnter the name: ";
	char nme[20];
	cin>>nme;
	fstream f;
	hotel ite;
	f.seekg(0);
	f.open("record.data",ios::in);
	do
			{
				f.read((char *)&ite,sizeof(hotel));
				if(f.eof()==1)
				{
					break;
				}
				if(strcmp(ite.name,nme)==0)
				{
	cout<<"\nName: ";
	cout<<ite.name;
	cout<<"\nRoom_id: ";
	cout<<ite.room_id;
	cout<<"\nPhone: ";
	cout<<ite.phone;
	cout<<"\nDate of booking: ";
	cout<<ite.wdate.d;
	cout<<"\nDate of check in: ";
	cout<<ite.cdate.d;
	cout<<"\nNo of days to stay:";
	cout<<ite.days;
	cout<<"\nRoom type (S=Single / D=Double /T= Tripple) bed: ";
	cout<<ite.room_type;
	cout<<"\nAddress: ";
	cout<<ite.address;
	cout<<"\nAdvance: ";
	cout<<ite.adv;
	cout<<endl;
				}
			}
			while(f);
	f.close();
	getch();
	clrscr();
	mainmenu();
}

void menus::viewmenu()
{       //function to display view menu
	clrscr();
	int co;
	fstream file;
	hotel item;
	cout<<"\n------------------------------";
	cout<<"\nViewing Record";
	cout<<"\n------------------------------";
	cout<<"\n1.View all Record Sequentially: ";
	cout<<"\n2.View all Single Rooms: ";
	cout<<"\n3.View all Double Rooms: ";
	cout<<"\n4.View all Tripple Rooms: ";
	cout<<"\n------------------------------";
	cout<<"\nEnter your choice: " ;
	cin>>co;
	clrscr();
	switch(co)
	{
		case 1:

	file.seekg(0);
	file.open("record.data",ios::in);
	while(file.read((char*)&item,sizeof(hotel)))
	{cout<<"\n---------------------------------";
	item.putdata();
	cout<<endl;
	}
	file.close();
	break;
	case 2:

	file.seekg(0);
	file.open("record.data",ios::in);
	while(file.read((char*)&item,sizeof(hotel)))
	{if(item.room_type=='S'){
		cout<<"\n---------------------------------";
	item.putdata();
	cout<<endl;
	}
	}
	file.close();
	break;
	case 3:

	file.seekg(0);
	file.open("record.data",ios::in);
	while(file.read((char*)&item,sizeof(hotel)))
	{if(item.room_type=='D'){
		cout<<"\n---------------------------------";
	item.putdata();
	cout<<endl;
	}
	}
	file.close();
	break;
	case 4:

	file.seekg(0);
	file.open("record.data",ios::in);
	while(file.read((char*)&item,sizeof(hotel)))
	{if(item.room_type=='T'){
		cout<<"\n---------------------------------";
	item.putdata();
	cout<<endl;
	}
	}
	file.close();
	break;
	default:cout<<"\nWrong choice.......";
	}
	getch();
	clrscr();
	mainmenu();
}
void menus::deletemenu()
{       //function to perform delete operation
	clrscr();
	fstream file;
	int no;
	hotel b1;
	cout<<"\n-------------------------";
	cout<<"\nDeleting Record";
	cout<<"\n-------------------------";
	file.open("record.data", ios::in|ios::binary) ;
	cout<<"\nEnter room no to delete data:";
	cin>>no;

    fstream temp ;
    temp.open("temp.data", ios::out|ios::binary) ;
    while ( !file.eof() )
    {
	file.read((char *)&b1, sizeof(hotel)) ;
	if ( file.eof() )
	    break ;
	if ( b1.room_id != no )
			{
			temp.write((char *)&b1, sizeof(hotel)) ;
			}



    }
    file.close() ;
    temp.close() ;
    remove("record.data");
	rename("temp.data","record.data");
	cout<<"\nRecord Deleted";
    getch();
    mainmenu();

}

int main()
{       clrscr();
	control c;
	c.controller();

return 0;
}
